﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using You.Niao.ShaFa.Model;
using Common.Base;
using DBUtility;
using You.Niao.ShaFa.IDAL;

namespace You.Niao.ShaFa.DAL
{
    public class PublishDAL:SqlConn,IPublish
    {
        public int Publish(Publish Pmodel)
        {
            string sql="insert into Publish(UserID,Province,City,Area,CutOffDate,Title,Description )values(@UserID,@Province,@City,@Area,@CutOffDate,@Title,@Description)";
            SqlParameter [] pars={ new SqlParameter("@UserID",Pmodel.UserID),
                                   new SqlParameter("Province",Pmodel.Province),
                                   new SqlParameter("City",Pmodel.City),
                                   new SqlParameter("Area",Pmodel.Area),
                                   new SqlParameter("CutOffDate",Pmodel.CutOffDate),
                                   new SqlParameter("Title",Pmodel.Title),
                                   new SqlParameter("Description",Pmodel.Description),
                                 };
                    return  DbHelperSQL.ExecuteSql(ShaFaConn,sql,pars); 
        }

        public IList<Publish> GetList(string City, int StartIndex, int EndIndex, out int RowsCount)
        {
            List<Publish> list = new List<Publish>();
            SqlParameter [] pars = {new SqlParameter("@City",City),
                                    new SqlParameter("@StartIndex",StartIndex),
                                    new SqlParameter("@EndIndex",EndIndex),
                                    new SqlParameter("@RowsCount",SqlDbType.Int,4)
                                    };

            pars[3].Direction = ParameterDirection.Output;
            using (SqlDataReader dr = DbHelperSQL.ExecuteReader(ShaFaConn, "Publish_Getlist",pars))
           {
               while (dr.Read())
               {
                   Publish p = new Publish();
                   p.CutOffDate = Convert.ToDateTime(dr["CutOffDate"]);
                   p.Title = dr["Title"].ToString();
                   p.UserID =Convert.ToInt32(dr["UserID"]);
                   p.NickName = dr["NickName"].ToString();
                   p.PublishID = Convert.ToInt32(dr["PublishID"]);
                   list.Add(p);
               }
           }
            if (pars[3].Value != DBNull.Value)
            {
                RowsCount = Convert.ToInt32(pars[3].Value);
            }
            else
            { 
                RowsCount = 0;
            }
            return list;
        }
    }
}
